﻿Ext.define('app.prm.project.PrmAudit', {
    extend: 'Ext.panel.Panel',
    controller: 'prmApply',
    requires: ['app.prm.project.PrmApplyController', 'app.prm.project.PrmApplyModel'], viewModel: {
        type: 'PrmApplyModel'
    },
    border: false,
    autoScroll: true,
    title: '',
    tbar: [
        { text: '通过', icon: '/Content/images/accept.png', handler: 'passProApp' },
        { text: '驳回', icon: '/Content/images/cancel.png', handler: 'rejectProApp' }
    ],
    layout: 'form',
    initComponent: function () {
        this.managerId = 0;
        this.items = [this.getForm(), this.getHistory()];
        this.callParent();
    },
    listeners: {
        'afterrender': 'loadAudit'
    },
    getGrid: function (status) {
        if (!this.historyGrid) {

            var expander = new Ext.grid.plugin.RowExpander({
                rowBodyTpl: '<p>申请说明：{appNote}' +
                '<br>审批状态：{appStatusTxt}' +
                 '<br>审批人员：{checkUsers}' +
                '<br>审核说明：{checkNote}'
            });

            this.historyGrid = new Ext.grid.GridPanel(
            {
                border: true,
                title: '审核历史',
                autoHeight: true, forceFit: true,
                store: new Ext.data.JsonStore(
                {
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (data) {
                                // Extract child nodes from the items or children property in the dataset
                                return data.result || data.children;
                            }
                        },
                        url: '/api/services/app/projectAp/GetApplyHistorys?id=' + this.paramsId
                    },
                    autoLoad: true,
                    fields: ['projectNo', { name: 'effectiveTime', type: 'date', dateFormat: 'c' },
                        { name: 'expirationTime', type: 'date', dateFormat: 'c' },
                        { name: 'createOn', type: 'date', dateFormat: 'c' },
                        { name: 'checkOn', type: 'date', dateFormat: 'c' },
                         'appNote', 'appStatusTxt', 'checkNote', 'checkUsers'],
                    listeners: {
                        'load': function (store, records) {
                            console.log('loading data');
                            console.log(records);
                        }
                    }
                }),
                plugins: expander,
                columns: [
                new Ext.grid.RowNumberer(),
                    { header: "项目编号", dataIndex: 'projectNo' },
                    { header: "生效时间", dataIndex: 'effectiveTime', maxWidth: 150, renderer: Ext.util.Format.dateRenderer('Y-m-d') },
                    { header: "过期时间", dataIndex: 'expirationTime', maxWidth: 150, renderer: Ext.util.Format.dateRenderer('Y-m-d') },
                    { header: "提交时间", dataIndex: 'createOn', maxWidth: 150, renderer: Ext.util.Format.dateRenderer('Y-m-d') },
                    expander
                ]
            });
        }
        return this.historyGrid;
    },
    getForm: function () {
        var main = this;
        if (!this.form) {
            this.form = new Ext.form.FormPanel(
            {
                border: false,
                frame: false,
                labelAlign: 'right',
                bodyStyle: 'padding:5px',
                items: [
                     {
                         xtype: 'displayfield',
                         fieldLabel: '申请时间',
                         bind: { value: '{_createon}' }
                     },
                    {
                        xtype: 'displayfield',
                        fieldLabel: '申请人',
                        bind: { value: '{appUser}' }
                    },
                    {
                        xtype: 'displayfield',
                        fieldLabel: '查看项目',
                        bind: { value: '{projectNo}' }
                    },
                    {
                        fieldLabel: '申请说明',
                        xtype: 'displayfield',
                        bind: { value: '{note}' }
                    },
                    this.getEffectiveTime(),
                     this.getExpirationTime(),
                    this.getTextArea()
                ]
            });
        }
        return this.form;
    },
    getEffectiveTime: function () {
        if (!this.effectTime) {
            this.effectTime = new Ext.form.DateField(
            {
                
                fieldLabel: '生效时间',
                format: 'Y-m-d',
                editable:false,
                bind: { value: '{effectiveTime}' }
            });
        }
        return this.effectTime;
    },
    getExpirationTime: function () {
        if (!this.expirTime) {
            this.expirTime = new Ext.form.DateField({
              
                fieldLabel: '过期时间',
                format: 'Y-m-d', editable: false,
                bind: { value: '{expirationTime}' }
            });
        }
        return this.expirTime;
    },
    getTextArea: function () {
        if (!this.textarea) {
            this.textarea = new Ext.form.TextArea({
                fieldLabel: '审核说明',
                height: 350, bind: { value: '{auditRemark}' },
                width: 840
            });
        }
        return this.textarea;
    },
    getHistory: function () {
        if (!this.history) {
            this.history = new Ext.Panel(
            {
                border: false,
                items: [this.getGrid()]
            });
        }
        return this.history;
    }
});